/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.service.base.exception;

import info.peigen.hotpot.common.core.enums.Messageable;
import info.peigen.hotpot.common.core.exception.SystemException;

/**
 * <b>(BusinessException)</b>
 * <p>
 * 标记事务可回滚的业务异常,配合声明式事务使用
 * 业务系统可以根据业务需求，继承该类定义具体业务相关的业务。如：NoFoundException, ParameterInvalidException 等。
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/4/9
 */
@SuppressWarnings("unused")
public class BusinessException extends SystemException implements Messageable {

    private String code = "Business";

    public BusinessException() {
        super();
    }

    public BusinessException(Messageable messageable) {
        super(messageable.message());
        this.code = messageable.code();
    }

    public BusinessException(String message) {
        super(message);
    }

    /**
     * @param message            错误消息
     * @param writableStackTrace 是否收集线程栈信息，对于业务明确的异常，请关闭,设置为false为关闭
     */
    public BusinessException(String message, boolean writableStackTrace) {
        super(message, null, false, writableStackTrace);
    }

    /**
     * @param code               错误码
     * @param message            错误消息
     * @param writableStackTrace 是否收集线程栈信息，对于业务明确的异常，请关闭,设置为false为关闭
     */
    public BusinessException(String code, String message, boolean writableStackTrace) {
        super(message, null, false, writableStackTrace);
        this.code = code;
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }

    public BusinessException(Throwable cause) {
        super(cause);
    }

    public BusinessException(String code, String message) {
        super(message);
        this.code = code;
    }

    public BusinessException(String code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public BusinessException(Throwable cause, String code) {
        super(cause);
        this.code = code;
    }

    public BusinessException(Messageable messageable, String msg) {
        super(msg);
        this.code = messageable.code();
    }

    public BusinessException(Messageable messageable, Throwable cause) {
        super(messageable.message(), cause);
        this.code = messageable.code();
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String code() {
        return this.code;
    }

    @Override
    public String message() {
        return this.getMessage();
    }
}